﻿using HHG.Common;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace OnLineExam.Web.mag.Compare
{
	/// <summary>
	/// userAnswerHandler 的摘要说明
	/// </summary>
	public class userAnswerHandler : IHttpHandler
	{

		private HttpContext _httpContext;
		private HttpRequest _httpRequest;
		private HttpResponse _httpResponse;
		private Data.OnlineExamEntities _ctx;
		private OnLineExam.Data.EfRepository<OnLineExam.Data.Score> _scoreRepository;
		private OnLineExam.Data.EfRepository<OnLineExam.Data.UserAnswer> _userAnswerRepository;
		public void ProcessRequest(HttpContext context)
		{
			_ctx = new Data.OnlineExamEntities();
			_httpContext = context;
			_httpRequest = context.Request;
			_httpResponse = context.Response;
			_scoreRepository = new Data.EfRepository<Data.Score>(_ctx);
			_userAnswerRepository = new Data.EfRepository<Data.UserAnswer>(_ctx);

			context.Response.ContentType = "text/plain";
			var type = context.Request.Params["type"];

			switch (type)
			{
				case "getlist":
					GetList();
					break;

				default:
					break;
			}
		}
		private void GetList()
		{

			var id = _httpRequest.Params["id"].GetInt();
			var qType = _httpRequest.Params["qtype"] ;//试题类型
			 
			var score = _scoreRepository.GetById(id);
			var answers = _userAnswerRepository.TableNoTracking.Where(s => s.ScoreID == id);

			using (var ctx = _ctx)
			{
				var spList = ctx.SingleProblem.Where(s => s.PaperID == score.PaperID).ToList();
				var mpList = ctx.MultiProblem.Where(s => s.PaperID == score.PaperID).ToList();
				var jpList = ctx.JudgeProblem.Where(s => s.PaperID == score.PaperID).ToList();
				var fpList = ctx.FillBlankProblem.Where(s => s.PaperID == score.PaperID).ToList();
				var qpList = ctx.QuestionProblem.Where(s => s.PaperID == score.PaperID).ToList();


				var query = from ua in ctx.UserAnswer
							where ua.ScoreID == id && ua.Type != "开始考试"  
							select ua;
				if (qType.HasValue())
					query = query.Where(s=>s.Type==qType);

				 
				var result = query.Select(ua=> new UsreAnswerView
				{
					ID = ua.ID,
					ScoreID = ua.ScoreID,
					PaperName = ua.Paper.PaperName,
					UserID = ua.UserID,
					UserMark = ua.UserMark,
					Mark = ua.Mark,
					UserAnswer1 = ua.UserAnswer1,
					TitleID = ua.TitleID,
					QuestionTypeStr = ua.Type,
					ExamTime = ua.ExamTime,

				} ).OrderBy(s=>s.QuestionTypeStr).ToList();
				 
				result.ForEach(m =>
				{
					m.QuestionType = Data.Paper.GetQuestionTypeEnum(m.QuestionTypeStr);
					switch (m.QuestionType)
					{
						case Data.QuestionType.Single:
							m.QuestionAnswer = spList.First(s => s.ID == m.TitleID).Answer;
							m.Title = spList.First(s => s.ID == m.TitleID).Title;
							break;
						case Data.QuestionType.Multi:
							m.QuestionAnswer = mpList.First(s => s.ID == m.TitleID).Answer;
							m.Title = mpList.First(s => s.ID == m.TitleID).Title;
							break;
						case Data.QuestionType.Judge:
							m.QuestionAnswer = jpList.First(s => s.ID == m.TitleID).Answer ? "正确" : "错误";
							m.Title = jpList.First(s => s.ID == m.TitleID).Title;
							break;
						case Data.QuestionType.Fill:
							m.QuestionAnswer = fpList.First(s => s.ID == m.TitleID).Answer;
							m.Title = fpList.First(s => s.ID == m.TitleID).Title;
							break;
						case Data.QuestionType.Qustion:
							m.QuestionAnswer = qpList.First(s => s.ID == m.TitleID).Answer;
							m.Title = qpList.First(s => s.ID == m.TitleID).Title;
							break;
						case Data.QuestionType.Other:
							break;
						default:
							break;
					}
				});


				WriteJson(EasyuiDataGrid.Create(result.Count(), result.ToList()));
			}




		}

		private class UsreAnswerView
		{
			public int ID { get; set; }
			public int ScoreID { get; set; }
			public string PaperName { get; set; }
			public string UserID { get; set; }
			public int UserMark { get; set; }
			public int Mark { get; set; }
			public string UserAnswer1 { get; set; }
			public int TitleID { get; set; }
			public string Title { get; set; }
			public Data.QuestionType QuestionType { get; set; }
			public string QuestionTypeStr { get; set; }
			public DateTime ExamTime { get; set; }
			/// <summary>
			/// 答案
			/// </summary>
			public string QuestionAnswer { get; set; }

		}
		private void WriteJson(object data)
		{
			_httpResponse.Write(data.SerializeObject());
			_httpResponse.End();
		}
		public bool IsReusable
		{
			get
			{
				return false;
			}
		}
	}
}